﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Mapping;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Data.Linq.Mapping;
using System.Reflection;
using System.Linq.Expressions;
namespace WEBDT.Base
{
    public class BaseModel:IModel
    {
        private DiDongDataContext db = new DiDongDataContext();
        public DiDongDataContext DB
        {
            get { return db; }
            set { db = value; }
        }

        public void commitchanges()
        {
            DB.SubmitChanges();
        }

        public IList<T> ListAll<T>() where T : class
        {
            var list = DB.GetTable<T>().ToList<T>();
            return list;
        }

        public T SelectByID<T>(int id) where T : class
        {
            var table = DB.GetTable<T>();
            MetaModel modelMap = table.Context.Mapping;
            ReadOnlyCollection<MetaDataMember> dataMembers
                = modelMap.GetMetaType(typeof(T)).DataMembers;

            string pk = (dataMembers.Single<MetaDataMember>
                (m => m.IsPrimaryKey)).Name;

            return table.SingleOrDefault<T>(delegate(T t)
            {
                String memberiD = t.GetType().GetProperty(pk).GetValue(t, null).ToString();
                return memberiD.ToString() == id.ToString();
            }
                );
        }

        public T SelectByID1<T>(string id) where T : class
        {
            var table = DB.GetTable<T>();
            MetaModel modelMap = table.Context.Mapping;
            ReadOnlyCollection<MetaDataMember> dataMembers
                = modelMap.GetMetaType(typeof(T)).DataMembers;

            string pk = (dataMembers.Single<MetaDataMember>
                (m => m.IsPrimaryKey)).Name;

            return table.SingleOrDefault<T>(delegate(T t)
            {
                String memberiD = t.GetType().GetProperty(pk).GetValue(t, null).ToString();
                return memberiD.ToString() == id;
            }
                );
        }

        public string GetPrimaryKeyName<T>() where T : class
        {
            var table = DB.GetTable<T>();
            MetaModel modelMapping = table.Context.Mapping;
            ReadOnlyCollection<MetaDataMember> dataMebers = modelMapping.GetMetaType(typeof(T)).DataMembers;

            return (dataMebers.Single<MetaDataMember>(m => m.IsPrimaryKey)).Name;
        }

        public void Insert<T>(T item) where T : class
        {
            DB.GetTable<T>().InsertOnSubmit(item);
            DB.SubmitChanges();
        }
        public UserPower kiemtratheoten(string up)
        {
            return DB.UserPowers.FirstOrDefault(h => h.TenUserPower == up);
        }

        public void Remove<T>(T item) where T : class
        {   
            DB.GetTable<T>().DeleteOnSubmit(item);
            DB.SubmitChanges();
        }

        public IQueryable<T> Danhsach<T>() where T : class
        {
            return DB.GetTable<T>().AsQueryable<T>();

        }
        
        public IQueryable<HoaDon> GetMaxIDHoaDon(string username) 
        {               
            var sv = from p in DB.HoaDons
                     where p.Username == username && p.MaHD== DB.HoaDons.Max(e=> e.MaHD)
                     select p;
            return sv.AsQueryable();
        }

        public IQueryable<LoaiSP> GetLoaiSPFromNhom(int NhomID)
        {
            var query = from dh in DB.LoaiSPs
                        where dh.MaNhom == NhomID
                        select dh;
            return query.AsQueryable();
        }

        public Account validateUser(string username, string password)
        {
            return DB.Accounts.FirstOrDefault(k => k.Username == username && k.Password == password);
        }
        public IList<SanPham> ListDT()
        {
            var query = from dh in DB.SanPhams
                        where dh.MaNhom ==1
                        select dh;
            return query.ToList();
           
        }
        public IList<SanPham> ListLap()
        {
            var query = from dh in DB.SanPhams
                        where dh.MaNhom == 2
                        select dh;
            return query.ToList();

        }
        public IList<SanPham> ListPK()
        {
            var query = from dh in DB.SanPhams
                        where dh.MaNhom == 4
                        select dh;
            return query.ToList();

        }
    }
}